package com.bianmin.coomen;
import com.google.gson.Gson;

import com.google.gson.reflect.TypeToken;
import shade.okhttp3.FormBody;
import shade.okhttp3.OkHttpClient;
import shade.okhttp3.Request;
import shade.okhttp3.Response;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

public class Util {

    //获取当前时间
    public static String currentTimeString() {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前时间为字符串
        return now.format(formatter);
    }

    //创建token
    public static String randomString(Integer length) {
        // 生成UUID
        UUID uuid = UUID.randomUUID();
        // 转换为字符串，并去掉中间的横线
        String randomString = uuid.toString().replace("-", "");
        // 截取前32位字符
        randomString = randomString.substring(0, length);
        return randomString;
    }

    //判断两个时间在不在同一天
    public static Boolean inSameDay(String dayOne, String dayTwo) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf.parse(dayOne);
        Date date2 = sdf.parse(dayTwo);
        // 判断两个时间是否在同一天
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        // 将日期设置到Calendar实例中
        cal1.setTime(date1);
        cal2.setTime(date2);

        // 比较年、月、日字段
        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
                && cal1.get(Calendar.DATE) == cal2.get(Calendar.DATE);
    }

    //获取置顶多少小时后的时间
    public static long timeStringForafterHour(int num) {
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();
        // 在当前时间基础上增加num小时
        calendar.add(Calendar.HOUR_OF_DAY, num);
        // 此时的calendar对象已经是num小时后的时间了，我们可以直接从这个Calendar对象获取时间戳
        // 将Calendar转换为时间戳(秒)
        return calendar.getTimeInMillis() / 1000;
    }

    //获取当前时间戳
    public static long currentTimesStamp() {
        return System.currentTimeMillis() / 1000;
    }

    public static Map<String,Object> postWithFormParameters(String url, Map<String, String> formParameters) throws IOException {

        OkHttpClient client = new OkHttpClient();

        // 创建 FormBody.Builder 的实例
        FormBody.Builder formBuilder = new FormBody.Builder();

        // 遍历 formParameters 中的参数，加入请求体
        for (Map.Entry<String, String> entry : formParameters.entrySet()) {
            formBuilder.add(entry.getKey(), entry.getValue());
        }

        System.out.println(formBuilder);

        FormBody body =  formBuilder.build();

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        // 发送请求并获取响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, Object>>(){}.getType();
            String responseBody = response.body().string();
            Map<String, Object> responseMap = gson.fromJson(responseBody, type);

            // 现在你有了一个Map，其中包含了你的JSON响应
            return responseMap;
        }
    }
}
